package com.gwt.tirso.gymkhana.server;

import java.util.List;

import javax.jdo.PersistenceManager;


import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.gwt.tirso.gymkhana.client.IRegistrationService;
import com.gwt.tirso.gymkhana.client.Person;

@SuppressWarnings("serial")
public class RegistrationServiceImpl extends RemoteServiceServlet implements
		IRegistrationService {
	
	
	//IPersonDAO pdao = new JDBCPersonDAO();

	@Override
	public Person registerUser(Person p) throws IllegalArgumentException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(p);
		}finally{
			pm.close();
		}
		//pdao.insertPerson(p);
		return p;
	}
	
	@Override
	public String checkEmail(String email){
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String s = null;
		List<Person> lp;
		
		try{
			String query = "select from " + Person.class.getName() + " where email == '" + email + "'";
			lp = (List<Person>) pm.newQuery(query).execute();
			if(lp.size() > 0){
				s = lp.get(0).getEmail();
			}
			
		}finally{
			pm.close();
		}
		return s;
	}



}
